package com.cms.recursoshumanos.popup;

import com.cms.recursoshumanos.cache.CarritoCompraCache;
import com.cms.recursoshumanos.component.base.CustomButtonColored;
import com.cms.recursoshumanos.component.base.CustomTextField;
import com.cms.recursoshumanos.constant.Constant;
import com.cms.recursoshumanos.object.ArticuloCarrito;
import com.cms.recursoshumanos.ui.catalogo.carrito.CarritoCompraScreen;

import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Screen;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.NumericChoiceField;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class EditCarritoPopUp extends PopupScreen
{
	
	private Screen elScreen;
	
	VerticalFieldManager vfm;
	NumericChoiceField ncf;
	VerticalFieldManager hfm;
	//TwoColumnField tcf;
	CustomTextField cantidad_pago;
	LabelField lblInfoTopPopUp_1,lblInfoTopPopUp_2,lblInfoTopPopUp_3;
	CustomButtonColored boton_ok, boton_cancel;
	
	private ArticuloCarrito elArticulo;
	
	int start = Constant.CANTIDAD_MINIMA_ARTICULO_EDIT;
	int end = Constant.CANTIDAD_MAXIMA_ARTICULO;
	int increment = Constant.STEP_CANTIDAD;
	int setTo =  Constant.NCF_SET_TO;
	
	
	
	public static EditCarritoPopUp instance = null;
	
	public static EditCarritoPopUp getInstance(ArticuloCarrito elArticulo, Screen elScreen)
	{		
		instance = new EditCarritoPopUp(elArticulo,elScreen);
		
		return instance;
	}
	public EditCarritoPopUp(ArticuloCarrito elArticulo, Screen elScreen)
	{
		super(new VerticalFieldManager(USE_ALL_WIDTH | FIELD_HCENTER), USE_ALL_WIDTH | FIELD_HCENTER);
		this.elArticulo = elArticulo;
		this.elScreen = elScreen;
		setComponents();
		
	}
	public void setComponents()
	{
		hfm = new VerticalFieldManager(FIELD_HCENTER);
		lblInfoTopPopUp_1 = new LabelField("Producto:",
											USE_ALL_WIDTH |
											FIELD_HCENTER |
											DrawStyle.HCENTER);
		
		lblInfoTopPopUp_2 = new LabelField("\""+this.elArticulo.getArticulo().getNombreArticulo()+"\"",
				USE_ALL_WIDTH |
				FIELD_HCENTER |
				DrawStyle.HCENTER);
		lblInfoTopPopUp_3 = new LabelField(" Cantidad a Cambiar?",
				USE_ALL_WIDTH |
				FIELD_HCENTER |
				DrawStyle.HCENTER);
		
		ncf = new NumericChoiceField("Cantidad:", start, end, increment,elArticulo.getCantidadSeleccionada())
		{

			protected int _drawPosition = -1;
			
			public void setDrawPosition( int drawPosition )
			{
				_drawPosition = drawPosition;
			}
			protected void drawFocus( Graphics g, boolean on )
			{
				if( _drawPosition < 0 ) {
					super.drawFocus( g, on );
				} 
				else 
				{
					boolean oldDrawStyleFocus = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS );
					try 
					{
						if( on ) 
						{
							g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, true );
						}
						paintBackground( g );
						paint( g );
					} 
					finally 
					{
						g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, oldDrawStyleFocus );
					}
				}
			}
			protected boolean keyChar( char character, int status, int time ) 
			{
				if( character == Characters.ENTER ) 
				{
					return super.navigationClick(status, time);
				}
				else if (character ==  Characters.ESCAPE)
	        	{
	        		close();
	        	}
				return super.keyChar( character, status, time );
				
			}
			
	
		};
		
		boton_ok = new CustomButtonColored("Aceptar Cambios"
				+ "",ButtonField.CONSUME_CLICK | FIELD_HCENTER, Constant.COLOR_BUTTON_FOCUS); 
		/*boton_cancel = new CustomButtonColored("Regresar"
				+ "",ButtonField.CONSUME_CLICK | FIELD_HCENTER, COLOR_FOCUS); 
        */
        boton_ok.setChangeListener(new FieldChangeListener() 
        {
	   		public void fieldChanged(Field field, int context) 
	       	{ 
	   			//	Dialog.ask == 4 -->> Presiono Si
	   			//	Dialog.ask == -1 -->> Presiono No

   				int cantidad_seleccionada = ncf.getSelectedValue();
	   			if (cantidad_seleccionada == elArticulo.getCantidadSeleccionada())
	   			{
	   				close();
	   			}
	   			else
	   			{
	   				if (Dialog.ask(Dialog.D_YES_NO, "Desea Aplicar los Cambios?") == 4)
		   			{
			   			int index = CarritoCompraCache.getInstance().getListaArticulo().getIndexArticulo(elArticulo);
			   			ShowDialogAlert alerta;
			   			if (cantidad_seleccionada == 0)
			   			{
			   				CarritoCompraCache.getInstance().getListaArticulo().deleteArticuloAtIndex(index);
			   				alerta = new ShowDialogAlert("Se ha Borrado Satisfactoriamente el Articulo ");
			   			}
			   			else
			   			{
			   				CarritoCompraCache.getInstance().getListaArticulo().
		   					getArticuloCarritoAtIndex(index).setCantidadSeleccionado(cantidad_seleccionada);
			   				alerta = new ShowDialogAlert("Se ha Modificado Satisfactoriamente el Articulo ");
			   			}
			   			alerta.run();
			   			if (elScreen instanceof CarritoCompraScreen)
			   			{
			   				((CarritoCompraScreen)elScreen).reset();
			   			}
			   			//elArticulo.setCantidadSeleccionado(0);
			   			close();
		   			}
	   			}
	   			
	   			
	       	} 
		}); 
       /* boton_cancel.setChangeListener(new FieldChangeListener() 
        {
	   		public void fieldChanged(Field field, int context) 
	       	{ 
	   			instance.close();
	       	} 
		});*/ 
     
        
		//tcf = new TwoColumnField(boton_ok,boton_cancel,USE_ALL_WIDTH);
		
		add(lblInfoTopPopUp_1);
		add(lblInfoTopPopUp_2);
		add(lblInfoTopPopUp_3);
		hfm.add(ncf);
		add(hfm);
		add(boton_ok);
		//hfm.add(boton_ok);
		//hfm.add(boton_cancel);
		//add(hfm);
		//add(tcf);
		
		
		//super(vfm , U);
	}
	// TODO Auto-generated constructor stub}

}
